#!/bin/sh
# togg, http://togg.googlecode.com/
#
# Copyright (c) 2007-2009 Stefan Ott. All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# exit codes:
# 0 - success (clever, I know)
# 1 - illegal argument
# 2 - environment not OK

# togg's version
VERSION=0.2.1

# the file for temporary data
TMPFILE=$( tempfile )
trap cleanup EXIT

cleanup() {
	rm -f ${TMPFILE}
}

# the function to call once the data is ready
FUNC=tag

# the placeholder for empty fields
NULL="__TOGG__NULL__VALUE__"

# default values
ARTIST=${NULL}
ALBUM=${NULL}
TITLE=${NULL}
TRACKNUMBER=${NULL}
DATE=${NULL}
GENRE=${NULL}
COMMENT=${NULL}

function show ()
{
	echo -n "${file}:"

	if [ $( wc -l < ${TMPFILE} ) -eq 0 ]; then
		echo " No tags"
	else
		echo
	fi

	sort < ${TMPFILE} | while read line
	do
		field=$( echo ${line} | cut -d '=' -f 1 )
		value=$( echo ${line} | cut -d '=' -f 2 )
		printf "%22s: %s\n" "${field}" "${value}";
	done
}

function del_unknown ()
{
	echo -n "Cleaning ${file}: "
	fields="artist album title tracknumber date genre comment"
	regex=^${fields// /|^}

	egrep "${regex}" "${TMPFILE}" > $TMPFILE.new
	mv ${TMPFILE}.new ${TMPFILE}

	vorbiscomment -w "${file}" < ${TMPFILE}
	echo done
}

function del_all ()
{
	echo -n "Untagging ${file}: "
	vorbiscomment -w "${file}" < /dev/null
	echo done
}

function tag ()
{
	echo -n "Tagging ${file}: "
	fields="artist album title tracknumber date genre comment"

	regex=^${fields// /|^}
	egrep -v "$regex" "${TMPFILE}" > $TMPFILE.new

	for field in ${fields} ; do
		readVar=$( echo ${field} | tr "[:lower:]" "[:upper:]" )
		eval "val=\$${readVar}"
		if [ "${val}" == "${NULL}" ] ; then
			len=$( echo ${field} | awk '{ print length($1) + 2 }' )
			val=$( grep -i ^${field}= ${TMPFILE} | cut -b ${len}- )
		fi
		echo "${field}=${val}" >> ${TMPFILE}.new
	done

	grep -v '=$' ${TMPFILE}.new > ${TMPFILE}

	vorbiscomment -w "${file}" < ${TMPFILE}
	echo done
}

function show_help ()
{
	me=$( echo $0 | sed 's:/.*/\([a-z]*\):\1:' )
	echo "Usage: ${me} -[tTaAycg] \`text' file1 [file2...]
       ${me} -[dDl] file1 [file2...]
       ${me} -[vh]
 -t   Modifies the Title tag
 -T   Modifies the Tracknumber tag
 -a   Modifies the Artist tag
 -A   Modifies the Album tag
 -y   Modifies the Date tag
 -c   Modifies the Comment tag
 -g   Modifies the Genre tag
 -l   Lists all tags
 -d   Deletes all tags
 -D   Deletes unknown tags
 -h   Displays this help info
 -v   Prints version info"
}

function show_version_info ()
{
	echo "This is togg v${VERSION}"
}

function check_env ()
{
	if [ ! -x "$( which vorbiscomment )" ] ; then
		echo "vorbiscomment not found - aborting" >&2
		exit 2
	fi
}

check_env

while getopts "a:A:t:T:y:g:c:lhvdD" flag 2>/dev/null; do
	case "$flag" in
		a)
			ARTIST=${OPTARG}
			;;
		A)
			ALBUM=${OPTARG}
			;;
		t)
			TITLE=${OPTARG}
			;;
		T)
			TRACKNUMBER=${OPTARG}
			;;
		y)
			DATE=${OPTARG}
			;;
		g)
			GENRE=${OPTARG}
			;;
		c)
			COMMENT=${OPTARG}
			;;
		l)
			FUNC=show
			;;
		d)
			FUNC=del_all
			;;
		D)
			FUNC=del_unknown
			;;
		v)
			show_version_info
			exit 0
			;;
		h)
			show_help
			exit 0
			;;
		?)
			show_help
			exit 1
			;;
	esac
done

if [ ${OPTIND} -eq 1 ]; then
	show_help
	exit 1
fi

shift $((OPTIND-1))

while [ ! -z "$1" ]; do
	file=$1
	shift

	vorbiscomment "${file}" > ${TMPFILE} || continue
	eval ${FUNC}
done
